package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.TakeGold.ApplyBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class TakeGoldActivity2 extends BaseActivity implements View.OnClickListener, TextWatcher {
    private static final String TAG = "TakeGoldActivity2";
    private static final int GET_CODE = 247;
    private TextView title;
    private TextView name_tv;//真实姓名
    private TextView id_card_tv;//身份证号
    private EditText phone_number_ed;
    private EditText code_ed;
    private Button get_code;

    private ApplyBean applyBean;

    private String phoneNumber;
    private String code;

    private int get_code_time = 0;
    private int way;//0自取 1配送

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_CODE:
                    if (get_code_time == 0){
                        get_code.setText("获取验证码");
                        setCodeText();
                    }else {
                        get_code_time--;
                        get_code.setText(get_code_time+"s后重新获取验证码");
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_gold2);
        way = getIntent().getIntExtra("way",0);
        applyBean = (ApplyBean) getIntent().getSerializableExtra("ApplyBean");
        initTirle();
        initView();
    }

    private void initView() {
        name_tv = (TextView) findViewById(R.id.name_tv);
        id_card_tv = (TextView) findViewById(R.id.id_card_tv);
        phone_number_ed = (EditText) findViewById(R.id.phone_number);
        code_ed = (EditText) findViewById(R.id.code);
        get_code = (Button) findViewById(R.id.get_code);

        get_code.setOnClickListener(this);
        phone_number_ed.addTextChangedListener(this);
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("取金币");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.next_step).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.get_code:
                code = code_ed.getText().toString();
                if (!MyUtils.isMobileNO(phoneNumber)) {
                    MyToast("请输入正确手机号");
                    return;
                }
                if (code == null || "".equals(code)){
                    MyToast("请输入验证码");
                    return;
                }


                break;
            case R.id.next_step:
                apply_take_gold();
                break;
        }
    }

    private void apply_take_gold(){
        if (way == 0){
            Map<String,Object> map = new HashMap<>();
            map.put("userId", MyApplication.getInstance().userBean.getUserId());
            map.put("goodId",applyBean.getGoodId());
            map.put("branchId",applyBean.getBranchId());
            map.put("number",applyBean.getNumber()+"");
            map.put("phoneNumber",phoneNumber);
            map.put("code",code);
            map.put("makeTime","2016-11-11");

            showDialog();
            XUtil.Post(ConfigUtil.APPLY_TAKE_GOLD,map,new MyCallBack<String>(){
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    Log.i(TAG, "onSuccess: "+result);
                    try {
                        JSONObject objent = new JSONObject(result);
                        String re = objent.optString("result");
                        if ("1".equals(re)){
                            startActivity(new Intent(TakeGoldActivity2.this, MyMain.class));
                        }else {
                            String info = objent.optString("info");
                            MyToast(info);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }



                @Override
                public void onFinished() {
                    super.onFinished();
                    Log.e(TAG, "onFinished: f" );
                    closeDialog();
                }
            });
        }else {

        }


    }

    private void setCodeText() {
        if (get_code_time == 0) {
            if (MyUtils.isMobileNO(phoneNumber)) {
                get_code.setSelected(true);
            } else {
                get_code.setSelected(false);
            }
        }

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        phoneNumber = phone_number_ed.getText().toString();
        setCodeText();

    }
}
